//
//  JXPMoviePlayer.m
//  模拟-百思不得姐
//
//  Created by ColinJ on 16/8/18.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "JXPMoviePlayer.h"

@implementation JXPMoviePlayer

static JXPMoviePlayer * _moviePlayerManager = nil;

+(instancetype)shareMoviePlayer{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _moviePlayerManager = [[self alloc]init];
    });
    return _moviePlayerManager;
}

- (instancetype)init
{
    if (self = [super init]) {
        _AVPlayer = [[AVPlayer alloc] init];
    }
    return self;
}

//播放视频
- (void)playViewWithURLString:(NSString *)urlString{
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:urlString]];
    [_AVPlayer replaceCurrentItemWithPlayerItem:item];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.AVPlayer];
    playerLayer.frame = self.playerView.bounds;
    playerLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;  //视频填充模式
    [self.playerView.layer addSublayer:playerLayer];
    [self playerPlay];
}

// 播放/切换
- (void)replaceItemWithUrlString:(NSString *)urlString
{
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:urlString]];
    [self.AVPlayer replaceCurrentItemWithPlayerItem:item];
    [self playerPlay];
}

- (void)playerPlay{
    [_AVPlayer play];
}

- (void)playerPause{
    [_AVPlayer pause];
}


@end
